package work;

import complexity.Stack;

import static java.lang.Integer.parseInt;

public class ReversePolishNotationTest {
    public static void main(String[] args) {
        String[] str = {"3","17","15","-","*","18","6","/","+"};
       int result =calculate(str);
        System.out.println(result);
    }

    private static int calculate(String[] str) {
        Stack<String> stack = new Stack<>();
        for (int i = 0; i < str.length; i++) {
            if (!(str[i].equals("-")||str[i].equals("/")||str[i].equals("+")||str[i].equals("*"))){
                stack.push(str[i]);
            }else {
                Integer one = Integer.valueOf(stack.pop());
                Integer two = Integer.valueOf(stack.pop());
                if ("-".equals(str[i])){
                    int three = two - one;
                    stack.push(String.valueOf(three));
                }else  if ("/".equals(str[i])){
                    int three = two / one;
                    stack.push(String.valueOf(three));
                }else  if ("*".equals(str[i])){
                    int three = two * one;
                    stack.push(String.valueOf(three));
                }else {
                    int three = one + two;
                    stack.push(String.valueOf(three));
                }
            }
        }
        Integer pop = Integer.valueOf(stack.pop());
        return pop;
    }
}
